package net.chowda.castcluster.action.ajax;

import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.chowda.castcluster.action.BaseAction;
import net.chowda.castcluster.VideoProvider;
import net.chowda.castcluster.Video;
import net.chowda.castcluster.playlist.PlayListManager;

import java.util.List;

/**
 * action for ajax requests related to previewing video sources and playlists
 */
public class VideoAction extends BaseAction {
    String videoUrl = null;
    String listId = null;

    /**
     * this handler renders the preview area.. if listId is set you get a preview for the entire list
     * otherwise videoUrl is used to render the preview area for that video.
     *
     * videoUrl is a CastCluster url
     *
     * the request will be populated with a "videos" attribute containing a list of 0 or more Video objects.
     */
    @DefaultHandler
    public Resolution preview() throws Exception {
        List<Video> videos;
        if(getVideoUrl() == null) {
            context.getRequest().setAttribute("listId", getListId());
            videos = PlayListManager.getVideosForList(PlayListManager.getPlayList(getListId()));
        } else {
            context.getRequest().setAttribute("videoUrl", getVideoUrl());
            videos = VideoProvider.getVideos(getVideoUrl());
        }
        context.getRequest().setAttribute("videos", videos);
        return new ForwardResolution("/view/video_preview.jsp");
    }

    public String getVideoUrl() {
        return videoUrl;
    }

    public void setVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
    }

    public String getListId() {
        return listId;
    }

    public void setListId(String listId) {
        this.listId = listId;
    }
}
